//
//  CompanyLookViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/30.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyLookViewController.h"
#import "ConpanyLookMeCell.h"
#import "WorkerModel.h"
#import "WorkerResumeDetailViewController.h"

@interface CompanyLookViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *itemDataArray;
    
    UITableView *viewTableview;
    NSInteger currentPage;
}
@end

@implementation CompanyLookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    itemDataArray = [NSMutableArray arrayWithCapacity:0];
    currentPage = 1;
    
    [self loadData];
    [self initUI];
}
- (void)loadData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:[NSString stringWithFormat:@"%ld", (long)currentPage] forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:CompanyMine_GetJobViewLists_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            for (NSDictionary *dic in data) {
                WorkerModel *model = [[WorkerModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                model.type = WorkerCellTypeOfLook;
                [self->itemDataArray addObject:model];
            }
            self.noDataView.hidden = YES;
            [self->viewTableview.mj_footer endRefreshing];
            [self->viewTableview reloadData];
        }
        if (self->itemDataArray.count) {
        } else {
            self.noDataView.hidden = NO;
            [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
        }
        SVPHUDDismiss;
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)initUI {
    viewTableview = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-HeightOfTabBar) target:self style:UITableViewStylePlain];
    viewTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    viewTableview.backgroundColor = COLOR_OF_WHITE;
    viewTableview.rowHeight = 118*Scale;
    [self.view addSubview:viewTableview];
    
    __weak __typeof(self)weakSelf = self;
    viewTableview.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self->currentPage++;
        [self->viewTableview.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (itemDataArray.count) {
        return itemDataArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ConpanyLookMeCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[ConpanyLookMeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
    }
    
    if (itemDataArray.count) {
        WorkerModel *model = itemDataArray[indexPath.row];
        [cell reloadDataModel:model];
    }
//    [cell reloadData];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WorkerModel *model = itemDataArray[indexPath.row];
    WorkerResumeDetailViewController *WRDVC = [[WorkerResumeDetailViewController alloc] init];
    WRDVC.id = model.resume_id;
    WRDVC.showChatSettingFlag = NO;
    WRDVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:WRDVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}

@end
